package de.hbrs.ooka.crs.servlet;

import de.hbrs.ooka.beans.ConferenceBeanStatelessLocal;
import de.hbrs.ooka.beans.PaperBeanStatelessLocal;
import de.hbrs.ooka.beans.UserBeanStatelessLocal;
import de.hbrs.ooka.beans.UserSessionStatefulLocal;
import de.hbrs.ooka.crs.servlet.constants.WebPageNames;
import de.hbrs.ooka.entities.ConferenceEntity;
import de.hbrs.ooka.entities.PaperEntity;
import de.hbrs.ooka.enums.Role;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AssignPaperServlet extends AbstractViewServlet {

    @EJB
    private PaperBeanStatelessLocal paperBean;

    @EJB
    private UserBeanStatelessLocal userBean;

    @EJB
    private ConferenceBeanStatelessLocal confBean;
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        UserSessionStatefulLocal crsBean = this.getCrsBean(request);

        RequestDispatcher rd = null;

        if (crsBean == null || !this.isUserAuthenticated(crsBean.getUser(), Role.ORGANIZER)) {
            request.setAttribute("message", "Not logged in");
            request.setAttribute("redirect", WebPageNames.LoginViewPage);
            rd = request.getRequestDispatcher("/" + WebPageNames.ErrorPage + ".jsp");
            if (rd != null) {
                rd.forward(request, response);
            }
        } else {
            String action = request.getParameter("action");

            if (action == null) {
                ConferenceEntity conf = this.confBean.getConference(request.getParameter("conference"));
                List<PaperEntity> paper = ( conf != null ? conf.getPapers() : new ArrayList<PaperEntity>());
                
                request.setAttribute("userlist", this.userBean.getAllUsers());
                request.setAttribute("paperlist", paper);
                rd = request.getRequestDispatcher("/" + WebPageNames.AssignPaperPage + ".jsp");
                if (rd != null) {
                    rd.forward(request, response);
                }
            } else if (action.equalsIgnoreCase("assignpapertoreviewer")) {
                
                String[] reviewers = request.getParameterValues("reviewer"); 
                String[] papers = request.getParameterValues("paper"); 
                if(reviewers.length > 0 && request.getParameter("paper") != null){
                    for (String reviewer : reviewers) {
                        for (String paper : papers) {
                            this.paperBean.assignToReviewer(this.userBean.getUser(Integer.parseInt(reviewer)), paper);
                        }
                    }
                }
                response.sendRedirect(WebPageNames.OrganizerViewPage);
            }
        }
    }
}
